import RPi.GPIO as gpio
import time
import sys

def init():
    gpio.setmode(gpio.BOARD)

    gpio.setup(7, gpio.OUT)
    gpio.setup(11, gpio.OUT)
    gpio.setup(13, gpio.OUT)
    gpio.setup(15, gpio.OUT)

def forward(tf):
    gpio.output(7, False)
    gpio.output(11, True)
    gpio.output(13, True)
    gpio.output(15, False)
    time.sleep(tf)
    gpio.cleanup()

def reverse(tf):
    gpio.output(7, True)
    gpio.output(11, False)
    gpio.output(13, False)
    gpio.output(15, True)
    time.sleep(tf)
    gpio.cleanup()

def turn_left(tf):
    gpio.output(7, True)
    gpio.output(11, True)
    gpio.output(13, True)
    gpio.output(15, False)
    time.sleep(tf)
    gpio.cleanup()

def turn_right(tf):
    gpio.output(7, False)
    gpio.output(11, True)
    gpio.output(13, False)
    gpio.output(15, False)
    time.sleep(tf)
    gpio.cleanup()

def turn_back(tf):
    gpio.output(7, True)
    gpio.output(11, False)
    gpio.output(13, True)
    gpio.output(15, False)
    time.sleep(0.1)
    gpio.cleanup()

def stop():
	gpio.cleanup()
	
def key_input(controller):
    init()
    print 'Key:', controller
    sleep_time = 0.2

    if controller == 'w':
        forward(sleep_time)
    elif controller == 's':
        reverse(sleep_time)
    elif controller == 'a':
        turn_left(sleep_time)
    elif controller == 'd':
        turn_right(sleep_time)
    elif controller == 'p':
        stop()
    elif controller == 'o':
	turn_back(sleep_time)
    else:
        stop()

